import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
  UseGuards,
  Query,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dto/create-article.dto';
import { UpdateArticleDto } from './dto/update-article.dto';
import {
  ApiBody,
  ApiOperation,
  ApiResponse,
  ApiTags,
  ApiParam,
  ApiQuery,
} from '@nestjs/swagger';
import { ListArticleDto } from './dto/list-article.dto';
import { Roles } from 'src/user/roles.decorator';
import { RolesGuard } from 'src/user/roles.guard';

@UseGuards(RolesGuard)
@Controller('article')
@ApiTags('文章管理')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @Post()
  @Roles('systemAdmin', 'admin', 'user')
  @ApiOperation({
    summary: '创建文章',
    description: '创建文章',
  })
  @ApiBody({ type: CreateArticleDto })
  @ApiResponse({ status: 200, description: '创建成功' })
  create(@Body() createArticleDto: CreateArticleDto, @Req() req) {
    return this.articleService.create(createArticleDto, req);
  }

  @Get()
  @ApiOperation({
    summary: '分页查询文章列表',
    description: '分页查询文章列表',
  })
  @ApiQuery({ name: 'query', type: ListArticleDto })
  @ApiResponse({ status: 200, description: '查询用户成功' })
  findAll(@Query() query: ListArticleDto) {
    return this.articleService.findAllByPage(query);
  }

  @Get(':id')
  @ApiOperation({
    summary: '根据ID查询文章详情',
    description: '根据ID查询文章详情',
  })
  @ApiParam({ name: 'id', description: '文章id' })
  @ApiResponse({ status: 200, description: '查询成功' })
  findOne(@Param('id') id: string) {
    return this.articleService.findOne(+id);
  }

  @Patch(':id')
  @Roles('systemAdmin', 'admin', 'user')
  @ApiOperation({
    summary: '根据ID修改文章',
    description: '根据ID修改文章',
  })
  @ApiParam({ name: 'id', description: '文章id' })
  @ApiBody({ type: UpdateArticleDto })
  @ApiResponse({ status: 200, description: '修改文章成功' })
  update(
    @Param('id') id: string,
    @Body() updateArticleDto: UpdateArticleDto,
    @Req() req,
  ) {
    return this.articleService.update(+id, updateArticleDto, req);
  }

  @Delete(':id')
  @Roles('systemAdmin', 'admin', 'user')
  @ApiOperation({
    summary: '删除文章',
    description: '根据id删除文章',
  })
  @ApiParam({ name: 'id', description: '文章id' })
  @ApiResponse({ status: 200, description: '删除文章成功' })
  remove(@Param('id') id: string, @Req() req) {
    return this.articleService.softDeleteArticle(+id, req);
  }
}
